package cn.peach.basic.controller;

import cn.peach.basic.util.AjaxResult;
import cn.peach.basic.util.FastDfsUtil;
import org.apache.ibatis.annotations.Delete;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {
    /**
     * 上传的接口
     * @param file
     * @return
     */
    @PostMapping
    public AjaxResult upload(@RequestPart(required = true,value = "file")MultipartFile file) {
        try {
            // 获取图片后缀
            String originalFilename = file.getOriginalFilename();
            System.err.println(originalFilename
            );
            String extName = originalFilename.substring(originalFilename.lastIndexOf(".") + 1);
            // 上传
            System.err.println(extName);
            String path = FastDfsUtil.upload(file.getBytes(), extName);
            return AjaxResult.me().subResultObj(path);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("卵系统");
        }
    }

    @DeleteMapping
    public AjaxResult delete(@RequestParam String path) {
        try {
            /**
             * localhost8080/fastDfs/1
             * 删除操作要先分割 可以先把/group1/asdfjsa/safsldf/sadfs.jpg这个的group前的“/”先分割掉
             *
             */
            // fsatDfs分割 这里分割group前的“/”
            String s = path.substring(1);
            // 将group1分割出来
            String group1 = s.substring(0, s.indexOf("/"));
            // 得到图片路径
            String remotePath = s.substring(s.indexOf("/") + 1);
            // 传入删除的组名和图片路径
            FastDfsUtil.delete(group1,remotePath);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage("烂系统");
        }
    }

}
